

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Transformers &mdash; NLP Architect by Intel® AI Lab 0.5.2 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
        <script type="text/javascript" src="_static/jquery.js"></script>
        <script type="text/javascript" src="_static/underscore.js"></script>
        <script type="text/javascript" src="_static/doctools.js"></script>
        <script type="text/javascript" src="_static/language_data.js"></script>
        <script type="text/javascript" src="_static/install.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="_static/nlp_arch_theme.css" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Mono" type="text/css" />
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:100,900" type="text/css" />
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="Additional Models" href="archived/additional.html" />
    <link rel="prev" title="Information Extraction" href="information_extraction.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="index.html">
          

          
            
            <img src="_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="quick_start.html">Quick start</a></li>
<li class="toctree-l1"><a class="reference internal" href="installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="tutorials.html">Jupyter Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="model_zoo.html">Model Zoo</a></li>
</ul>
<p class="caption"><span class="caption-text">NLP/NLU Models</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="tagging/sequence_tagging.html">Sequence Tagging</a></li>
<li class="toctree-l1"><a class="reference internal" href="sentiment.html">Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="bist_parser.html">Dependency Parsing</a></li>
<li class="toctree-l1"><a class="reference internal" href="intent.html">Intent Extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="lm.html">Language Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="information_extraction.html">Information Extraction</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Transformers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#base-model">Base model</a></li>
<li class="toctree-l2"><a class="reference internal" href="#models">Models</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#sequence-classification">Sequence classification</a></li>
<li class="toctree-l3"><a class="reference internal" href="#token-classification">Token classification</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="archived/additional.html">Additional Models</a></li>
</ul>
<p class="caption"><span class="caption-text">Optimized Models</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="quantized_bert.html">Quantized BERT</a></li>
<li class="toctree-l1"><a class="reference internal" href="transformers_distillation.html">Transformers Distillation</a></li>
<li class="toctree-l1"><a class="reference internal" href="sparse_gnmt.html">Sparse Neural Machine Translation</a></li>
</ul>
<p class="caption"><span class="caption-text">Solutions</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="absa_solution.html">Aspect Based Sentiment Analysis</a></li>
<li class="toctree-l1"><a class="reference internal" href="term_set_expansion.html">Set Expansion</a></li>
<li class="toctree-l1"><a class="reference internal" href="trend_analysis.html">Trend Analysis</a></li>
</ul>
<p class="caption"><span class="caption-text">For Developers</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="generated_api/nlp_architect_api_index.html">nlp_architect API</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer_guide.html">Developer Guide</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="index.html">NLP Architect by Intel® AI Lab</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="index.html">Docs</a> &raquo;</li>
        
      <li>Transformers</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="transformers">
<h1>Transformers<a class="headerlink" href="#transformers" title="Permalink to this headline">¶</a></h1>
<p>NLP Architect integrated the Transformer models available in <a class="reference external" href="https://github.com/huggingface/pytorch-transformers">pytorch-transformers</a>. Using Transformer models based on a pre-trained models usually done by attaching a classification head on the transformer model and fine-tuning the model (transformer and classifier) on the target (down-stream) task.</p>
<div class="section" id="base-model">
<h2>Base model<a class="headerlink" href="#base-model" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="generated_api/nlp_architect.models.transformers.html#nlp_architect.models.transformers.base_model.TransformerBase" title="nlp_architect.models.transformers.base_model.TransformerBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TransformerBase</span></code></a> is a base class for handling
loading, saving, training and inference of transformer models.</p>
<p>The base model support <cite>pytorch-transformers</cite> configs, tokenizers and base models as documented in their <a class="reference external" href="https://github.com/huggingface/pytorch-transformers">website</a> (see our base-class for supported models).</p>
<p>In order to use the Transformer models just sub-class the base model and include:</p>
<ul class="simple">
<li>A classifier (head) for your task.</li>
<li>sub-method handling of input to tensors used by model.</li>
<li>any sub-method to evaluate the task, do inference, etc.</li>
</ul>
</div>
<div class="section" id="models">
<h2>Models<a class="headerlink" href="#models" title="Permalink to this headline">¶</a></h2>
<p>Available transformer family models in NLP Architect:</p>
<table border="1" class="docutils">
<colgroup>
<col width="37%" />
<col width="9%" />
<col width="24%" />
<col width="7%" />
<col width="10%" />
<col width="13%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">&#160;</th>
<th class="head">BERT</th>
<th class="head">Quantized BERT</th>
<th class="head">XLM</th>
<th class="head">XLNet</th>
<th class="head">RoBERTa</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>Sequence classification</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
<td>Y</td>
</tr>
<tr class="row-odd"><td>Token classification</td>
<td>Y</td>
<td>Y</td>
<td>&#160;</td>
<td>Y</td>
<td>Y</td>
</tr>
</tbody>
</table>
<div class="section" id="sequence-classification">
<h3>Sequence classification<a class="headerlink" href="#sequence-classification" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="generated_api/nlp_architect.models.transformers.html#nlp_architect.models.transformers.sequence_classification.TransformerSequenceClassifier" title="nlp_architect.models.transformers.sequence_classification.TransformerSequenceClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">TransformerSequenceClassifier</span></code></a> is a transformer model with sentence classification head (the <code class="docutils literal notranslate"><span class="pre">[CLS]</span></code> token is used as classification label) for sentence classification tasks (classification/regression).</p>
<p>See <code class="docutils literal notranslate"><span class="pre">nlp_architect.procedures.transformers.glue</span></code> for an example of training sequence classification models on GLUE benchmark tasks.</p>
<p>Training a model on GLUE tasks, using BERT-base uncased base model:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>nlp-train transformer_glue <span class="se">\</span>
    --task_name &lt;task name&gt; <span class="se">\</span>
    --model_name_or_path bert-base-uncased <span class="se">\</span>
    --model_type bert <span class="se">\</span>
    --output_dir &lt;output dir&gt; <span class="se">\</span>
    --evaluate_during_training <span class="se">\</span>
    --data_dir &lt;/path/to/glue_task&gt; <span class="se">\</span>
    --do_lower_case
</pre></div>
</div>
<p>Running a model:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>nlp-inference run transformer_glue <span class="se">\</span>
    --model_path &lt;path to model&gt; <span class="se">\</span>
    --task_name &lt;task_name&gt; <span class="se">\</span>
    --model_type bert <span class="se">\</span>
    --output_dir &lt;output dir&gt; <span class="se">\</span>
    --data_dir &lt;path to data&gt; <span class="se">\</span>
    --do_lower_case <span class="se">\</span>
    --overwrite_output_dir
</pre></div>
</div>
<p>To run evaluation on the task’s development set add the flag <code class="docutils literal notranslate"><span class="pre">--evaluate</span></code>
to the command line.</p>
</div>
<div class="section" id="token-classification">
<h3>Token classification<a class="headerlink" href="#token-classification" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="generated_api/nlp_architect.models.transformers.html#nlp_architect.models.transformers.token_classification.TransformerTokenClassifier" title="nlp_architect.models.transformers.token_classification.TransformerTokenClassifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">TransformerTokenClassifier</span></code></a> is a transformer model for token classification for tasks such as NER, POS or chunking.</p>
<p>See example for usage <a class="reference internal" href="tagging/sequence_tagging.html#transformer-cls"><span class="std std-ref">TransformerTokenClassifier</span></a> NER model description.</p>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>